#include "agile.h"
#include "ui_agile.h"
#include "home.h"
#include "query.h"
#include "filter.h"
#include "predict.h"
#include "admin.h"
#include "detail.h"
#include "login.h"
#include "register.h"
#include "help.h"
#include "about.h"
#include "../utility/connector.h"
#include "../data/account.h"
#include "../data/evaluation.h"
#include "../utility/cache.h"
#include <QDesktopServices>
#include <QUrl>

Agile::Agile(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Agile) {
    qDebug()<<"Agile::Agile called.";

    ui->setupUi(this);

    account=new Account;
    evaluation=new Evaluation;
    evaluation->setScore(new QVector<QVector<double>>);
    evaluation->setTarget(new QStringList);
    evaluation->target->append("企业信用-信用水平");
    evaluation->target->append("企业信用-信用危机");
    evaluation->target->append("企业稳定性-企业能力");
    evaluation->target->append("企业稳定性-稳定水平");
    evaluation->target->append("企业规模背景-企业年龄");
    evaluation->target->append("企业规模背景-企业规模");
    evaluation->target->append("企业风险-司法风险");
    evaluation->target->append("企业风险-经营风险");
    evaluation->setMethod(new QStringList);
    evaluation->method->append("簇内误方差");
    evaluation->method->append("紧密性");
    evaluation->method->append("间隔性");
    evaluation->method->append("戴维森堡丁指数");
    evaluation->method->append("邓恩指数");
    evaluation->method->append("轮廓系数");
    evaluation->method->append("方差比标准");
    evaluation->method->append("方差");
    evaluation->method->append("样本标准偏差");
    evaluation->method->append("变异系数");
    evaluation->method->append("信息熵");
    evaluation->method->append("基尼指数");
    connector=new Connector(this);
    cache=new Cache(this,account,evaluation);

    widgetHome=new Home(this);
    widgetQuery=new Query(this,connector);
    widgetFilter=new Filter(this,connector);
    widgetPredict=new Predict(this,connector);
    widgetAdmin=new Admin(this,connector,evaluation);
    dialogLogin=new Login(this,connector,account);
    dialogRegister=new Register(this,connector,account);
    dialogHelp=new Help(this);
    dialogAbout=new About(this);

    ui->stackedWidget_main->addWidget(widgetHome);
    ui->stackedWidget_main->addWidget(widgetQuery);
    ui->stackedWidget_main->addWidget(widgetFilter);
    ui->stackedWidget_main->addWidget(widgetPredict);
    ui->stackedWidget_main->addWidget(widgetAdmin);
    ui->stackedWidget_main->setCurrentIndex(0);

    QObject::connect(ui->commandLinkButton_home,SIGNAL(clicked()),this,SLOT(home()));
    QObject::connect(ui->commandLinkButton_query,SIGNAL(clicked()),this,SLOT(query()));
    QObject::connect(ui->commandLinkButton_filter,SIGNAL(clicked()),this,SLOT(filter()));
    QObject::connect(ui->commandLinkButton_predict,SIGNAL(clicked()),this,SLOT(predict()));
    QObject::connect(ui->commandLinkButton_admin,SIGNAL(clicked()),this,SLOT(admin()));
    QObject::connect(ui->commandLinkButton_web,SIGNAL(clicked()),this,SLOT(web()));
    QObject::connect(ui->pushButton_login,SIGNAL(clicked()),this,SLOT(login()));
    QObject::connect(ui->pushButton_register,SIGNAL(clicked()),this,SLOT(enroll()));
    QObject::connect(ui->pushButton_help,SIGNAL(clicked()),this,SLOT(help()));
    QObject::connect(ui->pushButton_about,SIGNAL(clicked()),this,SLOT(about()));
    QObject::connect(ui->pushButton_refresh,SIGNAL(clicked()),this,SLOT(refresh()));

//    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
//    setFixedSize(this->width(),this->height()); // 禁止拖动窗口大小

    cache->load();
    loggedIn=cache->autoLogin;
    if(!cache->storePassword) account->setPassword(new QString(""));
    dialogLogin->autoLogin=cache->autoLogin;
    dialogLogin->storePassword=cache->storePassword;
    dialogLogin->push();
    widgetAdmin->push();

    updateVisibility();
    qDebug()<<"Agile::Agile returned.";
}

Agile::~Agile() {
    qDebug()<<"Agile::~Agile called.";
    cache->save();
    delete dialogAbout;
    delete dialogHelp;
    delete dialogRegister;
    delete dialogLogin;
    delete widgetAdmin;
    delete widgetPredict;
    delete widgetFilter;
    delete widgetQuery;
    delete widgetHome;
    delete cache;
    delete connector;
    delete evaluation;
    delete account;
    delete ui;
    qDebug()<<"Agile::~Agile returned.";
}

void Agile::updateVisibility() {
    qDebug()<<"Agile::updateVisibility called.";
    ui->commandLinkButton_home->setVisible(true);
    ui->commandLinkButton_web->setVisible(true);
    bool isAdmin=loggedIn && account->isAdmin;
    ui->commandLinkButton_query->setVisible(loggedIn);
    ui->commandLinkButton_filter->setVisible(loggedIn);
    ui->commandLinkButton_predict->setVisible(loggedIn);
    ui->commandLinkButton_admin->setVisible(isAdmin);
    qDebug()<<"Agile::updateVisibility returned.";
}

void Agile::login() {
    qDebug()<<"Agile::login called.";
    dialogLogin->exec();
    State &state=dialogLogin->state;
    if(state==State::ToRegister) enroll();
    else if(state==State::Succeeded) {
        loggedIn=true;
        updateVisibility();
        cache->autoLogin=dialogLogin->autoLogin;
        cache->storePassword=dialogLogin->storePassword;
        cache->save();
    }
    qDebug()<<"Agile::login returned.";
}

void Agile::enroll() {
    qDebug()<<"Agile::enroll called.";
    dialogRegister->exec();
    if(dialogRegister->state==State::ToLogin) login();
    qDebug()<<"Agile::enroll returned.";
}

void Agile::home() {
    qDebug()<<"Agile::home called.";
    ui->stackedWidget_main->setCurrentIndex(0);
    qDebug()<<"Agile::home returned.";
}

void Agile::query() {
    qDebug()<<"Agile::query called.";
    ui->stackedWidget_main->setCurrentIndex(1);
    qDebug()<<"Agile::query returned.";
}

void Agile::filter() {
    qDebug()<<"Agile::filter called.";
    ui->stackedWidget_main->setCurrentIndex(2);
    qDebug()<<"Agile::filter returned.";
}

void Agile::predict() {
    qDebug()<<"Agile::predict called.";
    ui->stackedWidget_main->setCurrentIndex(3);
    qDebug()<<"Agile::predict returned.";
}

void Agile::admin() {
    qDebug()<<"Agile::admin called.";
    ui->stackedWidget_main->setCurrentIndex(4);
    qDebug()<<"Agile::admin returned.";
}

void Agile::web() {
    qDebug()<<"Agile::web called.";
    QDesktopServices::openUrl(QUrl(QString ("http://127.0.0.1:5000/")));
    qDebug()<<"Agile::web returned.";
}

void Agile::help() {
    qDebug()<<"Agile::help() called.";
    dialogHelp->show();
    qDebug()<<"Agile::help() returned.";
}

void Agile::about() {
    qDebug()<<"Agile::about() called.";
    dialogAbout->show();
    qDebug()<<"Agile::about() returned.";
}

void Agile::refresh() {
    qDebug()<<"Agile::refresh() called.";
    int currentIndex=ui->stackedWidget_main->currentIndex();
    QWidget *widget=ui->stackedWidget_main->widget(currentIndex);
    if(widget==0) return;
    QWidget *newWidget=nullptr;
    switch (currentIndex) {
    case 0:
        break;
    case 1:
        newWidget=widgetQuery=new Query(this,connector);
        break;
    case 2:
        newWidget=widgetFilter=new Filter(this,connector);
        break;
    case 3:
        newWidget=widgetPredict=new Predict(this,connector);
        break;
    case 4:
        break;
    }
    if(newWidget==nullptr) return;
    ui->stackedWidget_main->removeWidget(widget);
    ui->stackedWidget_main->insertWidget(currentIndex,newWidget);
    delete widget;
    ui->stackedWidget_main->setCurrentIndex(currentIndex);
    qDebug()<<"Agile::refresh() returned.";
}
